using System;
using GameStateManagement.Input;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input.Touch;

namespace GameStateManagement.View
{
    class CameraTouchInputHandler : TouchInputHandler 
    {
        private readonly Camera _camera;

        public CameraTouchInputHandler(InputFsm machine, Camera camera)
            : base(machine)
        {
            _camera = camera;
        }

        protected override bool HandleTap(GestureSample gesture)
        {
            throw new NotImplementedException();
        }

        protected override bool HandleFreeDrag(GestureSample gesture)
        {
            var position = new Vector3(gesture.Position, 0);

            var delta = gesture.Delta == Vector2.Zero ? Vector2.One : gesture.Delta;
            
            var scale = Vector2.Distance(gesture.Position,
                                               gesture.Position + delta);
            
            _camera.Zoom(position, scale);
            
            return true;
        }
        

        protected override bool HandleDragComplete(GestureSample gesture)
        {
            throw new NotImplementedException();
        }

        protected override bool HandlePinch(GestureSample gesture)
        {
            throw new NotImplementedException();
        }

        public override bool HandleInput(InputState state)
        {
            bool inputHandled = false;
            foreach (var gesture in state.Gestures)
            {
                switch (gesture.GestureType)
                {
                    case GestureType.FreeDrag:
                        inputHandled = HandleFreeDrag(gesture);               
                        break;
                }
            }

            return inputHandled;
        }
    }
}
